// PGLZoomRegionEvent.cpp: implementation of the CPGLZoomRegionEvent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <pgl/mfc/pglmfcinclude.h>
#include <pgl/mfc/PGLZoomRegionEvent.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLZoomRegionEvent::CPGLZoomRegionEvent()
{

}

CPGLZoomRegionEvent::~CPGLZoomRegionEvent()
{

}


void CPGLZoomRegionEvent::OnMouseMove(UINT nFlags, CPoint point)
{
	int lastx,lasty;
	CPGLPointD scale;
	CPGLRectD rWorld; 

	if (!IsValid())
		return;

	// handle left button...
	if (GetMouse()->IsLeftBtnDown(lastx,lasty))
	{
		GetParent()->InvalidateRect(NULL,FALSE);
	}
} 

void CPGLZoomRegionEvent::OnLButtonUp(UINT nFlags, CPoint point) 
{
	int lastx, lasty, firstx, firsty;

	if (!IsValid())
		return;
	
	CPGLRectD rect = GetRegion()->GetArea().GetScreenBBox(*GetGfx());
	
	// handle left button...
	if (GetMouse()->IsLeftBtnDown(lastx,lasty))
	{
		// keeping information
		GetMouse()->GetLeftFirstPos(firstx,firsty);

		// checking that the box is not empty.
		if (
			(firstx == point.x) // x dimension = 0
			|| (firsty == point.y) // y dimension = 0
			)
			return;


		GetRegion()->ZoomBox(*GetGfx(), 
			firstx, 
			static_cast<int>(floor(firsty-rect.top)) ,
			point.x, 
			static_cast<int>(floor(point.y-rect.top))
			);

		GetHelper()->SetDirty();
		GetParent()->InvalidateRect(NULL,FALSE);
	}
}

void CPGLZoomRegionEvent::Draw(Gdiplus::Graphics& graphics, const CRect& clientRect)
{
	if (!GetMouse())
		return;

	using namespace Gdiplus;
	REAL dashVals[4];
	int lastx, lasty, firstx,firsty;

	// draw zoom region overlap...
	if (GetMouse()->IsLeftBtnDown(lastx,lasty))
	{
		Pen selectPen( Color( 0,0,0), 1);
		// Create and set an array of real numbers.
		dashVals[0] = 5;
		dashVals[1] = 3;
		dashVals[2] = 1;
		dashVals[3] = 3;
		selectPen.SetDashPattern( dashVals, 4);
		selectPen.SetWidth(2);
	
		// Set the dash pattern for the custom dashed line.
		GetMouse()->GetLeftFirstPos(firstx, firsty);
		graphics.DrawRectangle( &selectPen,  clientRect.left+ __min(firstx,lastx), clientRect.top+__min(firsty,lasty), abs(lastx - firstx), abs(lasty-firsty));
	}
}
